package edu.ybu.manage.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.pojo.Product;
import edu.ybu.manage.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/selectAll")
    public Map<String,Object> selectAll(Integer page,Integer limit){
        Integer count = productService.getCount();

        List<Product> productList = productService.selectAllProductByPage(page,limit);
//        List<Product> products = productService.selectAllProduct();

        System.out.println(productList);
        Map<String,Object> res = new HashMap<>();
        res.put("code",0);
        res.put("msg","");
        res.put("count",count);
        res.put("data",productList);
        return res;
    }

    @RequestMapping("/insertProduct")
    public Integer insertProduct(String json){
//        System.out.println(json);
        ObjectMapper objectMapper = new ObjectMapper();
        Product product = null;
        try {
            product = objectMapper.readValue(json, Product.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//        System.out.println(product);

        return productService.insertProduct(product);
    }

    @RequestMapping("/deleteProduct")
    public Integer deleteProduct(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Product product = null;
        try {
            product = objectMapper.readValue(json, Product.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return productService.deleteProduct(product);
    }

    @RequestMapping("/updateById")
    public Integer updateById(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Product product = null;
        try {
            product = objectMapper.readValue(json, Product.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return productService.updateProductById(product);
    }

    @RequestMapping("/searchProductsByPage")
    @ResponseBody
    public Map<String,Object> searchProductsByPage(Integer page, Integer limit, String json){
        //获得搜索的参数
        System.out.println(json);
        ObjectMapper objectMapper = new ObjectMapper();
        Product product = null;
        try {
            product = objectMapper.readValue(json, Product.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(product);
        //获取查询个数
        int count = productService.getSearchCount(product);
        //查询数据
        List<Product> productList = productService.searchProductsByPage(page, limit, product);
        //结果map
        Map<String,Object> res = new HashMap<>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", count);
        res.put("data", productList);
        return res;
    }

    @RequestMapping("/deleteProducts")
    public Integer deleteProducts(String json){
        System.out.println(json);
        ObjectMapper objectMapper = new ObjectMapper();
        Product[] products = new Product[0];
        try {
            products = objectMapper.readValue(json, Product[].class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        System.out.println(products);
        List<Integer> integers = new ArrayList<>();
        for (Product product : products) {
            Integer id = product.getId();
            integers.add(id);
        }
        return productService.deleteProducts(integers);
    }
}
